#!/usr/bin/env python
# -*- coding: utf-8 -*-

import csv
import os
import io

# 获取当前脚本的绝对路径
script_dir = os.path.dirname(os.path.abspath(__file__))

# 定义CSV文件路径和输出目录
csv_file = os.path.join(script_dir, '映射表_新索引.csv')
output_dir = script_dir

print(f"CSV文件路径: {csv_file}")
print(f"输出目录: {output_dir}")

# 检查CSV文件是否存在
if not os.path.exists(csv_file):
    print(f"错误：找不到CSV文件 {csv_file}")
    exit(1)

# 从CSV文件读取数据
try:
    with io.open(csv_file, 'r', encoding='utf-8') as f:
        reader = csv.reader(f)
        next(reader)  # 跳过标题行
        
        # 处理每一行数据
        for row in reader:
            if len(row) >= 7:  # 确保行有足够的列
                original_filename = row[0]  # 原文件名
                original_content = row[1]   # 原始内容
                renamed_file = row[2]       # 重命名文件
                variant1 = row[3]           # 变体1
                variant2 = row[4]           # 变体2
                variant3 = row[5]           # 变体3
                variant4 = row[6]           # 变体4
                
                # 创建txt文件，包含原始内容和四个变体
                output_file = os.path.join(output_dir, renamed_file)
                with io.open(output_file, 'w', encoding='utf-8') as txt_file:
                    txt_file.write(original_content + '\n')
                    txt_file.write(variant1 + '\n')
                    txt_file.write(variant2 + '\n')
                    txt_file.write(variant3 + '\n')
                    txt_file.write(variant4 + '\n')
                
                print(f"已生成文件: {renamed_file}")
    
    print("所有TXT文件已生成完成！")
except Exception as e:
    print(f"处理CSV文件时出错: {e}") 